package com.adcc.ags.cgw;

import com.adcc.ags.cgw.configuration.BlackListFactory;
import com.adcc.ags.cgw.configuration.JobFactory;
import com.adcc.ags.cgw.configuration.UserListFactory;
import com.adcc.ags.cgw.healthcheck.GWHealthCheck;
import com.adcc.ags.cgw.healthcheck.UserHealthCheck;
import com.adcc.ags.cgw.managed.downlink.DownlinkForward;
import com.adcc.ags.cgw.healthcheck.AMQSHealthCheck;
import com.adcc.ags.cgw.managed.proxy.JobProxy;
import com.adcc.ags.cgw.managed.proxy.MsgProxy;
import com.adcc.ags.cgw.managed.proxy.UserProxy;
import com.adcc.ags.cgw.resources.GWResource;
import com.adcc.ags.cgw.resources.UserResource;
import com.adcc.ags.cgw.managed.uplink.UplinkForward;
import com.adcc.utility.log.Log;
import com.google.common.collect.Lists;
import com.ibm.mq.MQException;
import io.dropwizard.Application;
import io.dropwizard.setup.Bootstrap;
import io.dropwizard.setup.Environment;
import org.glassfish.jersey.media.multipart.MultiPartFeature;

import java.io.File;
import java.util.List;

/**
 * CGW程序启动类
 */
public class App extends Application<AppConfiguration>{

    /**
     * 配置路径文件路径
     */
    public static String confPath = "";

    @Override
    public void initialize(Bootstrap<AppConfiguration> bootstrap) {
        super.initialize(bootstrap);
        bootstrap.addCommand(new ServerTraceCommand(this));
    }

    @Override
    public void run(AppConfiguration appConfiguration, Environment environment) {
        try {

            // 加载用户列表和黑名单列表
            UserListFactory.getInstance().load();
            BlackListFactory.getInstance().load();

            // 初始化MsgProxy
            MsgProxy.getInstance().build(environment, appConfiguration.getProxy());

            // 初始化UserProxy
            UserProxy.getInstance().build(environment, appConfiguration.getGwFactory());

            // 初始化下行处理模块
            DownlinkForward.getInstance().build(environment, appConfiguration.getAmqsFactory());

            // 初始化上行处理模块
            UplinkForward.getInstance().build(environment, appConfiguration.getGwFactory());

            // 初始化应急同步用户列表作业
            List<JobFactory> jobList = Lists.newArrayListWithExpectedSize(2);
            if(appConfiguration.getGwFactory().getEmergencyFactory() != null){
                jobList.add(appConfiguration.getGwFactory().getEmergencyFactory());
            }

            // 初始化GMS同步用户列表作业
            if(appConfiguration.getGwFactory().getGmsFactory() != null){
                jobList.add(appConfiguration.getGwFactory().getGmsFactory());
            }
            JobProxy.getInstance().build(environment,jobList);

            // 注册REST服务
            environment.jersey().packages("org.glassfish.jersey.media.multipart");
            environment.jersey().register(MultiPartFeature.class);
            environment.jersey().register(new UserResource());
            environment.jersey().register(new GWResource());

            // 注册状态网关监控
            environment.healthChecks().register("amqs", new AMQSHealthCheck());
            environment.healthChecks().register("gw", new GWHealthCheck());
            environment.healthChecks().register("gwuser", new UserHealthCheck());
            Log.info(App.class.getName(), "CGW running...");
        } catch (Exception ex) {
            Log.error(App.class.getName(), "run CGW failed",ex);
        }
    }

    public static void main(String[] args) throws Exception {
        String[] strParams = (args == null || args.length < 2) ? new String[]{"server","./CGW/conf/conf.yml"} : args;
        File file = new File(strParams[1]);
        confPath = file.getParent();
        MQException.log = null;
        App app = new App();
        app.run(strParams);
    }
}
